package nthu.web.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nthu.web.listener.LoginSessionListener;
import nthu.web.model.domain.User;


@SuppressWarnings("serial")
public class MainPageView extends ResourceView<User> {
	


	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
												throws ServletException, IOException {

		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Main Page</title>");
		
	    out.println("<link type=\"text/css\" href=\"css/structure/index.css\" rel=\"stylesheet\" />");
	    out.println("<link type=\"text/css\" href=\"css/theme/index.css\" rel=\"stylesheet\" />");

    	if(session.getAttribute("username")==null){
    		
    	    out.println("</head>");
        	out.println("<body>"); 
        	/* ---------if we didn't login , no need javascript---------*/

    		out.println("Please Login here<br/>");
    		out.println("<a href=\"./login\">Login</a><br/>");

    	}else{
    	    out.println("<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\"></script>");
    	    out.println("<script type=\"text/javascript\" src=\"js/common.js\"></script>");

    	    out.println("<script type=\"text/javascript\" src=\"js/autogrow.js\"></script>");
    	    out.println("<script type=\"text/javascript\" src=\"js/onload.js\"></script>");
    	    out.println("<script type=\"text/javascript\" src=\"js/loadbox.js\"></script>");
    	    out.println("<script type=\"text/javascript\" src=\"js/loadcurrentuser.js\"></script>");
    		
    	    out.println("</head>");
        	out.println("<body>");
        	/* ---------load javascript after login---------*/
        	
    		out.println("<div id=\"leftbox\">");
    		
    			out.println("Login username: "+session.getAttribute("username")+" ， Welcome！<br/>");
    			out.println("<b>" + session.getId() + "</b><br/>" +
    				    "session timeout period: <b>" + session.getMaxInactiveInterval() + "</b><br/><br/>");

    			out.println("<div id=\"mainbox-select\">生活軌跡		附近事件		購物		美食旅遊</div>");

/*
    			out.println("Now changing it to 20 minutes.<br><br>");
    			session.setMaxInactiveInterval(20 * 60);
    			out.println("The SESSION TIMEOUT period is now "
        		+ session.getMaxInactiveInterval() + " seconds.");
*/

    			out.println("<div id=\"mainbox\">");
    			out.println("<div id=\"list-area\"></div>");
    			out.println("</div>");
    			
    		out.println("</div>"); //end left box


    		out.println("<div id=\"rightbox\">");
    		
    			out.println("<div id=\"more-info\"></div>"); // 未來使用
    			out.println("<div id=\"map-show-info\"></div>");
    			out.println("<div id=\"map-info\"></div>");
    			out.println("<script src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>");
    			/* 是否有使用 GPS 等感測設備 */
    			out.println("<script type=\"text/javascript\" src=\"js/geolocation.js\"></script>");


        		// LoginSessionListener don't need to new , only one in pipezon 
        		out.println("線上已經創建session總數(線上使用者)"+LoginSessionListener.getCount());
        	   /*
        		Map<String,String> map = LoginSessionListener.getMap();
        		
                 Collection<String> collection = map.values();
                Iterator<String> iterator = collection.iterator();
                while(iterator.hasNext()) {
                    out.println(iterator.next());
                }
        		
        		*/
        		
    		out.println("</div>");



    		out.println("<div id=\"footer\">");

    		out.println("<div class=\"inputopacity\">");
    			out.println("<form id=\"tweetForm\">");

    				out.println("<div class=\"label-textarea\">What are you doing?</div>");
    				out.println("<div id=\"magicInput\"><textarea name=\"magicInputArea\" id=\"magicInputArea\" rows=\"2\" cols=\"10\"></textarea></div>");
    				out.println("<div  class=\"countnum\">140</div>");
    				out.println("<input class=\"submitButton inact\" name=\"submit\" type=\"submit\" value=\"update\" disabled=\"disabled\" />");
    				out.println("<div class=\"clear\"></div>");
    				
    			out.println("</form>");
    		out.println("</div>");
    		
    		out.println("</div>");
    		

    	}

    	out.println("</body>");
    	out.println("</html>");


   }




}
